package com.frxuexi.soufang

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.core.view.setMargins
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.GlideUtil
import com.frxuexi.lib_base.entity.HourseDBBean
import com.frxuexi.lib_base.entity.HourseDetailBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.soufang.databinding.ActivityHourseDetailBinding
import com.google.android.flexbox.FlexboxLayout
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/soufang/HourseDetailActivity")
@AndroidEntryPoint
class HourseDetailActivity : BaseActivity<ActivityHourseDetailBinding,HourseViewModel> (){

    @JvmField
    @Autowired
    var id:String?=null

    var flag=false
    lateinit var hourse:HourseDetailBean
    override fun initData() {
        vm.hourseDetailSuc.observe(this){
            hourse=it
            vm.selectSC(hourse.id.toInt())
            Log.i("===",it.toString())
            GlideUtil.loadImg(it.indexPictureUrl,binding.detailIv)
            it.labelDesc?.split("|")?.forEach {
                val tv = TextView(this)
                tv.apply {
                    setText(it)
                    setBackgroundResource(R.drawable.shape_back)
                    setPadding(20,5,20,5)
                    layoutParams= FlexboxLayout.LayoutParams(
                        FlexboxLayout.LayoutParams.WRAP_CONTENT,
                        FlexboxLayout.LayoutParams.WRAP_CONTENT
                    ).apply {
                        setMargins(10)
                    }
                }
                binding.detailFlex.addView(tv)
            }
            binding.detailTitle.setText(it.title)
            binding.detailPrice.setText("${it.unitPrice}元/月")
            binding.detailRoom.setText("${it.bedRoom}室${it.livingRoom}厅")
            binding.detailPf.setText("${it.area}平")
            binding.detailZx.append(it.decoration)
        }

        vm.addHourseSuc.observe(this){
            binding.detialSc.setBackgroundResource(R.drawable.ic_action_zan)
        }
        vm.deleteHourseSuc.observe(this){
            binding.detialSc.setBackgroundResource(R.drawable.ic_action_nozan)
        }

        vm.selectSCSuc.observe(this){
            if(it>0){
                binding.detialSc.setBackgroundResource(R.drawable.ic_action_zan)
                flag=true
            }else{
                binding.detialSc.setBackgroundResource(R.drawable.ic_action_nozan)
                flag=false
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.getHourseDetail(id?.toInt()!!)



        binding.detialSc.setOnClickListener {
            val hourseDBBean = HourseDBBean(
                hourse.id.toLong(),
                hourse.indexPictureUrl,
                hourse.labelDesc,
                hourse.title,
                hourse.unitPrice,
                "${hourse.bedRoom}室${hourse.livingRoom}厅",
                hourse.area
            )
            if(flag){//已经收藏过，取消收藏
                vm.deleteHourse(hourseDBBean)
            }else{//没有收藏过，做收藏操作
                vm.addHourse(hourseDBBean)
            }
            flag=!flag
        }
    }

}